package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.UserCommunicationImgs;
import com.qzct.entity.vo.CommunicationVo;
import com.qzct.service.UserCommunicationImgsService;
import com.qzct.service.UserService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.UserCommunication;
import com.qzct.service.UserCommunicationService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-01-11
 */
@RestController
@Api(tags = "种草")
@RequestMapping("/userCommunication")
public class UserCommunicationController {

    @Autowired
    private UserCommunicationService userCommunicationService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserCommunicationImgsService userCommunicationImgsService;

    /**
     * 添加。
     *
     * @param userCommunication 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("") CommunicationVo communicationVo) {
        ArrayList<UserCommunicationImgs> imgs = communicationVo.getImgs();
        userCommunicationService.save(communicationVo.getUserCommunication());
        int id = userCommunicationService.getMaxid();
        for (int i = 0; i < imgs.size(); i++) {
            imgs.get(i).setCommunicationId(id);
            userCommunicationImgsService.save(imgs.get(i));
        }
        return true;
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return userCommunicationService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param userCommunication 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") UserCommunication userCommunication) {
        return userCommunicationService.updateById(userCommunication);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<CommunicationVo> list() {
        ArrayList<CommunicationVo> list = new ArrayList<>();
        List<UserCommunication> list1 = userCommunicationService.list();
        for (int i = list1.size()-1; i >= 0; i--) {
            CommunicationVo communicationVo = new CommunicationVo();
            communicationVo.setUserImg(userService.getById(list1.get(i).getUserId()).getImage());
            communicationVo.setUsername(userService.getById(list1.get(i).getUserId()).getName());
            communicationVo.setUserCommunication(userCommunicationService.getById(list1.get(i).getId()));
            communicationVo.setImgs(userCommunicationImgsService.getByCid(list1.get(i).getId()));
            list.add(communicationVo);
        }
        return list;
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public UserCommunication getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return userCommunicationService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<UserCommunication> page(@ApiParam("分页信息") Page<UserCommunication> page) {
        return userCommunicationService.page(page);
    }

}
